<template>
  <!-- 表单 -->
  <el-form label-width="120px" ref="form" :model="formData" :rules="rules">
    <el-form-item prop="username" label="姓名">
      <el-input
        v-model="formData.username"
        style="width:50%"
        placeholder="请输入姓名"
      />
    </el-form-item>
    <el-form-item prop="mobile" label="手机">
      <el-input
        v-model="formData.mobile"
        style="width:50%"
        placeholder="请输入手机号"
      />
    </el-form-item>
    <el-form-item prop="timeOfEntry" label="入职时间">
      <el-date-picker
        v-model="formData.timeOfEntry"
        style="width:50%"
        placeholder="请选择入职时间"
      />
    </el-form-item>
    <el-form-item prop="formOfEmployment" label="聘用形式">
      <el-select
        v-model="formData.formOfEmployment"
        style="width:50%"
        placeholder="请选择"
        value=""
      >
        <el-option
          v-for="(item, index) in hireType"
          :key="index"
          :label="item.value"
          :value="item.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="workNumber" label="工号">
      <el-input
        v-model="formData.workNumber"
        style="width:50%"
        placeholder="请输入工号"
      />
    </el-form-item>
    <el-form-item prop="departmentName" label="部门">
      <el-input
        v-model="formData.departmentName"
        style="width:50%"
        placeholder="请选择部门"
        @focus="departmentNameFocus"
      />
      <!-- readonly 只读 -->
      <el-tree
        :data="treeData"
        default-expand-all
        :props="{ label: 'name' }"
        v-show="showTree"
        @node-click="treeClick"
      ></el-tree>
    </el-form-item>
    <el-form-item prop="correctionTime" label="转正时间">
      <el-date-picker
        v-model="formData.correctionTime"
        style="width:50%"
        placeholder="请选择转正时间"
      />
    </el-form-item>
    <el-form-item>
      <el-button @click="cancelfn">取消</el-button>
      <el-button type="primary" @click="confirmfn">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import constant from '@/api/constant'
export default {
  name: 'EmpDialog',
  props: {
    treeData: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      showTree: false, // 部门下拉列表展示与否
      hireType: constant.hireType,
      clickDepartName: '', // 点击的部门名字
      // 添加表单字段
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    // 检测-用户在页面修改后赋予新的值
    // 检测-点击树形控件时,给此对象属性赋值也会触发此函数
    // 检测-reserFields置空时也触发了
    'formData.departmentName' (newval) {
      if (newval.length === 0) {
        // 证明要置空
        return // 组织代码往下执行
      }
      if (newval !== this.clickDepartName) {
        // 证明用户在输入框修改
        this.formData.departmentName = this.clickDepartName
      }
    }
  },
  methods: {
    cancelfn () {
      this.$emit('update:show', false)
    },
    confirmfn () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$emit('addEmev', { ...this.formData })
          this.$emit('update:show', false)
        }
      })
    },
    // 树形控件-点击事件
    treeClick (data) {
      // 如果当前部门还有子部门，则不能被选中
      if (data && data.children) {
        return
      }
      // 把当前选中的节点显示在 input 框中
      this.formData.departmentName = data.name
      this.showTree = false
      this.clickDepartName = data.name
    },
    // 部门输入框聚焦事件
    departmentNameFocus () {
      this.showTree = true
    }
  }
}
</script>

<style lang="scss" scoped></style>
